package com.milhouz.bomberrokon.sprite;

import com.milhouz.bomberrokon.model.BomberData;
import com.milhouz.bomberrokon.model.CollisionDetector;
import com.stickycoding.Rokon.Texture;

/**
 * Bomber sprite
 */
public class BomberSprite extends SolidObject {

	private static final int ANIME_STOP = 1;
	private static final int ANIME_DOWN[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
	private static final int ANIME_LEFT[] = { 12, 13, 14, 15, 16, 17, 18, 19 };
	private static final int ANIME_RIGHT[] = { 20, 21, 22, 23, 24, 25, 26, 27 };
	private static final int ANIME_UP[] = { 28, 29, 30, 31, 32, 33, 34, 35, 36 };
	private static final int ANIME_DIE[] = { 37, 38, 39, 40 };

	public static final int DIRECTION_STOP = 0;
	public static final int DIRECTION_UP = 1;
	public static final int DIRECTION_DOWN = 2;
	public static final int DIRECTION_LEFT = 3;
	public static final int DIRECTION_RIGHT = 4;

	private static final int ANIM_FRAME_TIME = 100;

	// ---- Bomber caracteristiques
	private int mBomberId = 0;
	private final BomberData mBomberData = new BomberData();
	private boolean died = false;
	private boolean showTexture = true;

	/**
	 * 
	 * @param id
	 *            Id of the bomber
	 * @param centerX
	 *            Center X coordonate of the bomber
	 * @param centerY
	 *            Center Y coordonate of the bomber
	 * @param texture
	 *            bomber textures
	 */
	public BomberSprite(int id, float centerX, float centerY, Texture texture) {
		super(centerX, centerY, texture);
		mBomberId = id;
		if (!showTexture) {
			super.resetTexture();
		}
		// super.setTileIndex(ANIME_STOP);
	}

	public void move(int moveCode, CollisionDetector collisions) {
		int newMoveCode = collisions.checkBomberCollisions(this, moveCode);
		if (newMoveCode != -1) {
			switch (newMoveCode) {
			case DIRECTION_STOP: // stop
				stopBomber();
				break;
			case DIRECTION_UP: // key Up
				// if (!isDirection(DIRECTION_UP)) {
				// super.animateCustom(ANIME_UP, ANIM_FRAME_TIME);
				// }
				if (getVelocityY() == 0) {
					setVelocityY(-mBomberData.getSpeed());
				}
				mSolidDirection = DIRECTION_UP;
				break;
			case DIRECTION_DOWN: // key Down
				// if (!isDirection(DIRECTION_DOWN)) {
				// super.animateCustom(ANIME_DOWN, ANIM_FRAME_TIME);
				// }
				if (getVelocityY() == 0) {
					setVelocityY(mBomberData.getSpeed());
				}
				mSolidDirection = DIRECTION_DOWN;
				break;
			case DIRECTION_LEFT: // key Left
				// if (!isDirection(DIRECTION_LEFT)) {
				// super.animateCustom(ANIME_LEFT, ANIM_FRAME_TIME);
				// }
				if (getVelocityX() == 0) {
					setVelocityX(-mBomberData.getSpeed());
				}
				mSolidDirection = DIRECTION_LEFT;
				break;
			case DIRECTION_RIGHT: // key Right
				// if (!isDirection(DIRECTION_RIGHT)) {
				// super.animateCustom(ANIME_RIGHT, ANIM_FRAME_TIME);
				// }
				if (getVelocityX() == 0) {
					setVelocityX(mBomberData.getSpeed());
				}
				mSolidDirection = DIRECTION_RIGHT;
				break;
			}
		} else {
			mSolidDirection = moveCode;
		}
	}

	public boolean isDirection(int directionCode) {
		return mSolidDirection == directionCode;
	}

	public int getBomberId() {
		return mBomberId;
	}

	public void killBomber() {
		stopBomber();
		died = true;
		// super.animateCustom(ANIME_DIE, ANIM_FRAME_TIME, 2, false);
		// super.setTileIndex(ANIME_STOP);
		super.setVelocity(0, 0);
	}

	public boolean isDeadBomber() {
		return died;
	}

	public void stopBomber() {
		// super.stopAnimation();
		// super.setTileIndex(ANIME_STOP);
		super.setVelocity(0, 0);
		mSolidDirection = DIRECTION_STOP;
	}

	public BomberData getBomberData() {
		return mBomberData;
	}

	public boolean canDropBomb() {
		return mBomberData.canDropBomb() && !isDeadBomber();
	}

}
